package thread;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * <pre>演示并发下不安全的ArrayList</pre>
 *
 * @author YangHang
 * @Date 2019/3/22 10:20
 */
public class NoSafeArrayList {

    static List<String> list = new CopyOnWriteArrayList<>(new ArrayList<>());


    static class NoSafeThread implements Runnable {

        @Override
        public void run() {
                for (int i = 0; i < 200; i++) {
                    list.add("YH");
                }
        }
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(new NoSafeThread());
        Thread t2 = new Thread(new NoSafeThread());
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(NoSafeArrayList.list.size());
    }
}